import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'
import { getDetail } from '@/api/detail'
import { idStore } from '@/stores/goodsId'
import {rquestAddCart} from '@/api/addCart'
export const useDeail = ()=>{
const router = useRouter()
const list = ref({
    category_id: 0,
    detail: '',
    id: 0,
    oldprice: 0,
    picture: '',
    price: 0,
    product: '',
    shop: '',
})
const count = idStore()
let id = count.getGoodsId
const toHome = () => {
    router.push('/home')
}
const toCart = () => {
    router.push('/cart')
}

const getDetailData = async () => {
    const data: any = await getDetail(id)
    const { resultCode, resultInfo } = data
    if (resultCode === 1) {
        list.value = resultInfo
        
    }
}
const onSubmit =async () => {
    const data:any = await rquestAddCart(list.value.id)
    const { resultCode} = data
    if (resultCode === 1) {
        Toast('添加购物车成功')
    }else{
        Toast('添加购物车失败')
    }
}
onMounted(() => {
    getDetailData()
})
return {
    list,
    toHome,
    toCart,
    onSubmit
}
}